package com.example.manage_dzensaas.service.mq;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.manage_dzensaas.mapper.db2.ConsumerOrderMapper;
import com.example.manage_dzensaas.mapper.db2.ConsumerOrderQxMapper;
import com.example.manage_dzensaas.mapper.db2.IdempotentQueryMapper;
import com.example.manage_dzensaas.pojo.entity.db2.ConsumerOrder;
import com.example.manage_dzensaas.pojo.entity.db2.ConsumerOrderQx;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.solon.annotation.Db;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 共享服务
 *
 * @author wangzhiqi
 */
@Component
public class ServiceCommon {

    private static final Logger LOGGER = LoggerFactory.getLogger(ServiceCommon.class);

    @Db("db2")
    IdempotentQueryMapper idempotentQueryMapper;
    @Db("db2")
    ConsumerOrderMapper consumerOrderMapper;
    @Db("db2")
    ConsumerOrderQxMapper consumerOrderQxMapper;

    /**
     * 获取主订单信息
     *
     * @param orderId 订单号
     * @return ConsumerOrder
     */
    public ConsumerOrder getOrderInfo(String orderId) {
        return consumerOrderMapper.selectById(orderId);
    }

    /**
     * 获取权益订单信息
     *
     * @param orderId 订单号
     * @return ConsumerOrder
     */
    public ConsumerOrderQx getQxOrderInfo(String orderId) {
        return consumerOrderQxMapper.selectOne(new LambdaQueryWrapper<ConsumerOrderQx>().eq(ConsumerOrderQx::getParentOrderId, orderId));
    }

    /**
     * 判断订单是否重复消费
     *
     * @param orderId 订单号
     * @return boolean
     */
    public boolean judgeIdempotence(String orderId, int businessType) {
        int i = idempotentQueryMapper.selectByStatus(orderId, businessType);
        return i > 0;
    }

    /**
     * 判断参数是否为空
     *
     * @param param 参数
     * @return boolean
     */
    public boolean paramNotNull(String param) {
        if (StringUtils.isEmpty(param)) {
            return Boolean.TRUE;
        } else {
            return Boolean.FALSE;
        }
    }
}